#!/usr/bin/env python
#coding:utf-8
#读取金刚经中的文字
#ver 2：将Ver1改编而成，能成功运行了

import os
import urllib2
from sgmllib import SGMLParser

class URLLister(SGMLParser):
    '''HTML解析类
    '''
    def reset(self):
        SGMLParser.reset(self)
        self.urls = []

    def start_a(self, attrs):
        '''解析出HTML文件中含有的链接
        '''
        href = [v for k, v in attrs if k=='href']
        if href:
            self.urls.extend(href)  #追加到URL列表
            
class Save:
    '''读取，并保存到文本
    '''
    def __init__(self, urls):
        '''初始化Save类'''        
        self.parser = URLLister()
        self.urls = urls
        
    def readIndex(self, urls):
        '''读取首面内容到文件'''
        html = self.openUrls(urls)
        self.saveToFile(html)
        print 'Index file to save!'

    def seHttpDocs(self, urls):
        '''读取首页中所链到的页面到文件'''
        html = self.openUrls(urls)
        self.parser.feed(html)
        for url in self.parser.urls:
            myurl = self.urls + url
            print 'get:' + myurl
            html = self.openUrls(myurl)
            self.saveToFile(html, url)
        
    def saveToFile(self, htmlSource, url='jgh.html'):
        '''保存到文件'''
        print "Save as:" + url
        if not os.path.isdir('tt/'):
            os.mkdir("tt")
        f = file(r"tt/" + url, 'w')
        f.write(htmlSource)
        f.close()

    def openUrls(self,urls):
        '''打开Url链接'''
        sock = urllib2.urlopen(urls)
        httpSource = sock.read()
        sock.close()
        return httpSource

    def main(self):
        '''用于测试的执行程序'''
        #self.readIndex(self.urls)
        self.seHttpDocs(self.urls)

if __name__ == '__main__':
    app = Save(r"http://www.sinc.sunysb.edu/Clubs/buddhism/JinGangJingShuoShenMo/")
    app.main()
